$(document).ready(function () {
	//监测浏览窗口动态变化
	$(window).resize(function () {
		amend_modal_place( $('#add_user_modal'));//修正弹窗位置  始终处于屏幕中央  添加人员
		amend_modal_place( $('#department_modal_tip'));//修正弹窗位置  始终处于屏幕中央  必填信息提示

		mdoal_select_length_speace_give_val();//弹层  添加人员  下拉选择框 位置、长度赋值
	});

	//设置当前页面选中样式
	setting_style_the_page();
	function setting_style_the_page() {
		var first_menu_item = $('#sidebar-nav').find('a[href="#accounts-menu-first-01"]');//获取一级菜单可点击的a标签
		var img_nav = first_menu_item.find('.define-nav');//获取导航图片
		var img_left = first_menu_item.find('.define-left');//获取转向图片
		var first_ul = $('#accounts-menu-first-01');//获取一级下拉菜单
		var first_ul_li_one = first_ul.children('li').eq(3);//获取  "黑名单管理"

		var second_menu_item = $('#sidebar-nav').find('a[href="#accounts-menu-second-04"]');//获取二级菜单可点击的a标签
		var second_img_left = second_menu_item.parent().find('.define-left');//获取转向图片
		var second_ul = $('#accounts-menu-second-04');//获取二级下拉菜单
		var second_ul_li_one = second_ul.children('li').eq(1);//获取  "权限管理"

		first_menu_item.addClass('define-active');//一级菜单添加左边横线
		img_nav.attr('src','/statics/images/sever_hospital_select.png');//切换导航图片
		img_left.addClass('define-rotate-active');//添加动画类
		first_ul.addClass('in');//展拉一级菜单
		first_ul_li_one.addClass('define-active');//li添加左边横线

		second_ul.addClass('in');//展开二级菜单
		second_img_left.addClass('define-rotate-active');//添加动画类
		second_ul_li_one.addClass('define-active');//"患者账号管理"  添加选中样式
	}

	//分页切换效果
	page_style_switch();
	function page_style_switch() {
		var page_ = $('.define-paging');
		var the_first_page = $('#the_first_page');//获取首页
		var prev_page = $('#prev_page');//获取上一页
		var next_page = $('#next_page');//获取下一页
		var the_last_page = $('#the_last_page');//获取尾页

		// 123456
		page_.delegate('.define-page','click',function () {
			page_.find('.define-page').removeClass('define-active');
			$(this).addClass('define-active');
		});

		// 上一页
		prev_page.on('click',function () {
			var active_page = page_.find('.define-active');
			if(active_page.text() > 1){
				page_.find('.define-page').removeClass('define-active');
				active_page.prev().addClass('define-active')
			}
		});

		// 下一页
		next_page.on('click',function () {
			var active_page = page_.find('.define-active');
			if(active_page.text() < 5){
				page_.find('.define-page').removeClass('define-active');
				active_page.next().addClass('define-active')
			}
		});

		// 首页
		the_first_page.on('click',function () {
			page_.find('.define-page').removeClass('define-active');
			page_.find('.define-page').eq(0).addClass('define-active');
		});

		// 尾页
		the_last_page.on('click',function () {
			page_.find('.define-page').removeClass('define-active');
			page_.find('.define-page').eq(4).addClass('define-active');
		});

	}

	//弹层  修正弹窗位置  始终处于屏幕中央
	function amend_modal_place($modal) {
		var window_width = $(window).width();
		var window_height = $(window).height();
		var width = $modal.width();
		var height = $modal.height();

		var left = ( window_width - width ) / 2;
		var top  = ( window_height - height ) / 2;

		$modal.css('left',left);
		$modal.css('top',top);
	}

	//点击添加  show添加人员弹层
	show_add_user_modal();
	function show_add_user_modal() {
		$('#admin_add_people').on('click',function () {
			mdoal_select_length_speace_give_val();//弹层  添加人员  下拉选择框 位置、长度赋值
			amend_modal_place( $('#add_user_modal') );//修正弹窗位置  始终处于屏幕中央  添加人员
			$('#add_user_modal').modal({keyboard:false});
		});
	}

	//弹层  添加人员  下拉选择框 位置、长度赋值
	function mdoal_select_length_speace_give_val() {
		var modal_select_btn = $('#modal_select_btn');//获取基类
		var modal_select_admin = $('#modal_select_admin');//获取下拉选择框
		var window_width = $(window).width();//获取屏幕长度

		if(window_width >= 1024){
			modal_select_admin.css( 'width',modal_select_btn.outerWidth() - 2 );
			modal_select_admin.css( 'top' , modal_select_btn.outerHeight() + 1 );
			modal_select_admin.css( 'maxHeight' , modal_select_admin.find('div').outerHeight() * 4 );
		}
		if(window_width >= 1100){
			modal_select_admin.css( 'width',modal_select_btn.outerWidth() - 2 );
			modal_select_admin.css( 'top' , modal_select_btn.outerHeight() + 1 );
			modal_select_admin.css( 'maxHeight' , modal_select_admin.find('div').outerHeight() * 4 );
		}
		if(window_width >= 1366){
			modal_select_admin.css( 'width',modal_select_btn.outerWidth() - 2 );
			modal_select_admin.css( 'top' , modal_select_btn.outerHeight() + 1 );
			modal_select_admin.css( 'maxHeight' , modal_select_admin.find('div').outerHeight() * 4 );
		}
		if(window_width >= 1920){
			modal_select_admin.css( 'width',modal_select_btn.outerWidth() - 2 );
			modal_select_admin.css( 'top' , modal_select_btn.outerHeight() + 1 );
			modal_select_admin.css( 'maxHeight' , modal_select_admin.find('div').outerHeight() * 4 );
		}
	}

	//弹层  点击选择科室  展示下拉选择框  及动画效果
	modal_show_select();
	function modal_show_select() {
		var btn = $('#modal_select_btn');//获取点击框
		var modal_select_admin = $('#modal_select_admin');//获取下拉选择框

		btn.on('click',function () {
			if(modal_select_admin.hasClass('active')){
				btn.next().attr('src','/statics/images/sever_unretrieve.png');
				modal_select_admin.animate({
					height: 'toggle'
				},500,function () {
					modal_select_btn_show_radius(btn);//添加人员弹窗  选择按钮  添加圆角
				});
				modal_select_admin.removeClass('active');//隐藏弹层
			}else {
				btn.next().attr('src','/statics/images/sever_retrieve.png');
				modal_select_admin.animate({
					height: 'toggle'
				},500);
				modal_select_btn_hide_radius(btn);//添加人员弹窗  选择按钮  消除圆角
				modal_select_admin.addClass('active');//显示弹层
			}
		});
	}

	//弹层  获取科室
	modal_show_get_depart_function();
	function modal_show_get_depart_function() {
		var modal_select_admin = $('#modal_select_admin');
		$.ajax({
			url: '/patient/getDepartList',
			type: 'POST',
			dataType: 'json',
			data: {},
			success:function(data){
				modal_select_admin.empty();
				if( data.code == '0' ){
					for( var i = 0 ; i < data.data.length ; i++ ){
						modal_select_admin.append( '<div id="'+ data.data[i].id +'">'+ data.data[i].de_name +'</div>' )
					}
				}
			},
			error: function (err) {
				console.log(err);
			}
		})
	}

	//弹层  点击选择科室  展示下拉选择框  选择科室
	modal_show_select_function();
	function modal_show_select_function() {
		var btn = $('#modal_select_btn');//获取点击框
		var modal_select_admin = $('#modal_select_admin');//获取下拉选择框
		modal_select_admin.delegate('div','click',function () {
			var self = $(this);
			modal_select_admin.find('div').removeClass('active');
			self.addClass('active');
			btn.val( self.text() );
			btn.next().attr('src','/statics/images/sever_unretrieve.png');
			modal_select_admin.animate({
				height: 'toggle'
			},500,function () {
				modal_select_btn_show_radius(btn);//添加人员弹窗  选择按钮  添加圆角
			});
			modal_select_admin.removeClass('active');
		});
	}

	//添加人员弹窗  选择按钮  消除圆角
	function modal_select_btn_hide_radius($btn) {
		$btn.css('borderBottomLeftRadius','0');
		$btn.css('borderBottomRightRadius','0');
	}

	//添加人员弹窗  选择按钮  添加圆角
	function modal_select_btn_show_radius($btn) {
		$btn.css('borderBottomLeftRadius','3px');
		$btn.css('borderBottomRightRadius','3px');
	}

	//添加人员弹层  必填选项验证
	modal_verify_must_write();
	function modal_verify_must_write() {
		var input = $('#add_user_modal').find('input');
		$('#modal_kt').on('click',function () {
			if( !input.eq(0).val() || !input.eq(1).val() ){
				amend_modal_place( $('#department_modal_tip'));//修正弹窗位置
				$('#department_modal_tip').modal({keyboard:false});//弹出弹层
				$('body').children('.modal-backdrop:last-child').css('zIndex','1050');//修改弹层层级
			}else {
				//提交表单
				modal_sure_add_function();//弹层  确认添加功能  form表单赋值
			}
		});
	}

	//弹层  确认添加功能  form表单赋值
	function modal_sure_add_function() {
		var form = $('<form id="add_user_limit_form" style="display: none;"></form>');
		form.append('<input type="text" name="admin" style="display: none;">');//科室
		form.append('<input type="text" name="name" style="display: none;">');//姓名
		$('body').append( form );
		var input = $('#add_user_modal').find('input');

		form.find('input[name="admin"]').val( $('#modal_select_admin').find('.active').attr('id') );/*科室*/
		form.find('input[name="name"]').val( input.eq(1).val() );/*姓名*/

		mdal_sure_add_function_ajax( form.serialize() );//弹层  确认添加功能 ajax
	}

	//弹层  确认添加功能 ajax
	function mdal_sure_add_function_ajax(data) {
		$.ajax({
			url: '/patient/addBlackManage',
			type: 'POST',
			dataType: 'json',
			data: {
				postData: data
			},
			success:function(data){
				console.log(data);
				if( data.code == '0' ){
					window.location = location.href;
					$('#add_user_modal').modal('hide');
				}
			},
			error: function (err) {
				console.log(err);
			}
		})
	}

	/*启用、禁用功能*/
	used_and_unused_function();
	function used_and_unused_function() {
		var table = $('#table_handle');
		table.delegate('.define_used','click',function () {
			var self = $(this);
			var status = '';
			if( $.trim( self.text() ) == '启用' ){
				status = '0';
			}else {
				status = '1';
			}

			$.ajax({
				url: '/patient/setBlackManageStu',
				type: 'POST',
				dataType: 'json',
				data: {
					id: self.attr('data_id'),
					status: status
				},
				success:function(data){
					if( data.code == '0' ){
						if( $.trim( self.text() ) == '启用' ){
							self.html('<img src="/statics/images/sever_disabled.png" alt="">' + '禁用');
						}else {
							self.html('<img src="/statics/images/sever_undisabled.png" alt="">' + '启用');
						}
					}
				},
				error: function (err) {
					console.log(err);
				}
			});
		});
	}

});